import {createSlice} from "@reduxjs/toolkit";

//创建分片
const userSclice = createSlice({
    name:"userSclic", //action分组
    initialState:{
        isLogin:false, //用户是否登录，默认false
        userMenu:[],   //用户菜单
        userInfo:{},   //用户信息：用户名，icon
    },
    // reducer对象，中放 reducer函数
    reducers:{
        editLoginStatus(state,action){
            console.log("修改登录状态...")
            state.isLogin = action.payload;
        },
        changMenu(state,action){
            console.log('准备保存...')
            state.userMenu = action.payload;
            // Redux Toolik 默认已经集成了immmer.js库，数组、对象的修改同基本类型一样，直接赋值。
        },
        saveUserInfo(state,action){
            let {payload} = action; //action = {type:"",payload:{uname,avatar}},解构 获取参数 
            state.userInfo = {uname:payload.uname,avatar:payload.avatar};
        },
        clearUserInfo(state,action){
            console.log("清空...")
            state.userInfo={}//清空
        }
    }
})

// 规定：action生成器函数的名称同 reduer函数名称
export const {editLoginStatus,changMenu,saveUserInfo,clearUserInfo} = userSclice.actions; //action 生成器
export default userSclice.reducer; //导出reducer